/**
 * 
 */
package tests;

import hibernateUtils.HibernateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;



import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;

import subscribersManagement.Entitled;
import subscribersManagement.Subscriber;

import daos.DAOs;
import daos.MediaDAO;




import static org.junit.Assert.*;


import exceptions.BadParametersException;
import exceptions.ChildLoanException;
import exceptions.ExtensionBackDateException;
import exceptions.LentMediaException;
import exceptions.ReservationMediaException;
import exceptions.SubscriberExistsException;


import exceptions.MediaExistsException;

import loansManagement.Check;
import loansManagement.Loan;
import loansManagement.LoanId;
import loansManagement.LoanReminder;
import loansManagement.Reservation;
import main.Library;
import mediasManagement.Book;
import mediasManagement.Dvd;
import mediasManagement.Exemplary;
import mediasManagement.FabricCategory;
import mediasManagement.Media;


/**
 * @author virgil
 *
 */
public class LibraryTests {
	
	private Library lib;
	private FabricCategory fabric;
	private Media b,b1,b2,b3,b4;
	private Media d;
	private EntityManager em;
	private Subscriber s,s1,s2,s3,s4;
	private Entitled e;

	private ArrayList<String> authors;
	private ArrayList<String> categories;
	
	@Before
	public void beforeTests() throws BadParametersException, MediaExistsException
	{
		lib = new Library();
		fabric=new FabricCategory();
		

		authors = new ArrayList<String>();
		categories= new ArrayList<String>();
		authors.add("Antony");
		categories.add("Informatique");
		categories.add("java");
		b = new mediasManagement.Book("le Mystère du code", authors, categories, new GregorianCalendar(),"2012744",3,fabric);
		b1 = new mediasManagement.Book("le Mystère du code", authors, categories, new GregorianCalendar(),"20127448",3,fabric);
		b2 = new mediasManagement.Book("le Mystère du code_1", authors, categories, new GregorianCalendar(),"20127454",fabric);
		b3=new mediasManagement.Book("livre  3", authors, categories, new GregorianCalendar(),"20127450",fabric);
		b4=new mediasManagement.Book("livre  3", authors, categories, new GregorianCalendar(),"20127452",fabric);
		d= new Dvd("le Mystère du code en DVD", authors, categories, new GregorianCalendar(),"2012746",3,fabric);

		s= new Subscriber("Iullia","Petrova", new GregorianCalendar(1987, Calendar.JULY, 10), new GregorianCalendar(2012, Calendar.MAY, 02),
				"romain.tocci@telecom-bretagne.eu");
		s1=new Subscriber("Romain","TOCCI", new GregorianCalendar(1988, Calendar.MARCH, 15), new GregorianCalendar(2011, Calendar.JUNE, 14),"tocci.romain@tb");
		s2=new Subscriber("Phillipe","JEAN", new GregorianCalendar(1994, Calendar.JANUARY, 10), new GregorianCalendar(2011, Calendar.MARCH, 16),"phillipe.jean@tb");
		s3=new Subscriber("Virgil","ADOPO", new GregorianCalendar(1965, Calendar.MARCH, 13), new GregorianCalendar(2010, Calendar.AUGUST, 24),"");
		e= new Entitled("ADOPO", "Alexandre", new GregorianCalendar(2010, Calendar.APRIL, 02), new GregorianCalendar(2012, Calendar.MAY, 3),null, s);
		
	}
	

	
	@Test
	public void testLoanPersistence1() throws BadParametersException, MediaExistsException,
											  SubscriberExistsException, LentMediaException, ChildLoanException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		Media u= DAOs.getMediaDAO().get(b.getMediaNumberId());
		Subscriber v= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(v.getCurrentloans().size()==0);
		assertTrue(u.getNumberAvailableExemplary()==3);
		t = session.beginTransaction();	
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		Media x= DAOs.getMediaDAO().get(b.getMediaNumberId());
		Subscriber y= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(y.getCurrentloans().size()==1);
		assertTrue(x.getNumberAvailableExemplary()==2);
		assertTrue(x.getLentExemplariesNumber()==1);
		t.commit();
		HibernateUtil.closeSession();
		
	}
	@Test//(expected = ChildLoanException.class)
	public void testLoanPersistence2() throws BadParametersException, MediaExistsException,
											  SubscriberExistsException, LentMediaException, ChildLoanException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(d);
		s.getEntitles().add(e);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		Media u= DAOs.getMediaDAO().get(d.getMediaNumberId());
		Subscriber v= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(e.getLastName());
		assertTrue(v.getCurrentloans().size()==0);
		assertTrue(u.getNumberAvailableExemplary()==3);
		t = session.beginTransaction();	
		lib.lend(e, d);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		Media x= DAOs.getMediaDAO().get(d.getMediaNumberId());
		Subscriber y= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(e.getLastName());
		assertTrue(y.getCurrentloans().size()==0);
		assertTrue(x.getNumberAvailableExemplary()==3);
		assertTrue(x.getLentExemplariesNumber()==0);
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test
	public void testreturn() throws BadParametersException, MediaExistsException,
											  SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		Media u= DAOs.getMediaDAO().get(b.getMediaNumberId());
		Subscriber v= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(v.getCurrentloans().size()==0);
		assertTrue(u.getNumberAvailableExemplary()==3);
		t = session.beginTransaction();	
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
//		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		Subscriber p= DAOs.getSubscriberDAO().GetSubscriberById(s.getNumber());
		assertTrue(p.getNumberOfBookLoans()==1);
		Scanner sc = new Scanner(System.in);
		System.out.println("Veuillez saisir un l'id de l'exemplaire:");
		String str = sc.nextLine();
		lib.returnMedia(str);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		

		Subscriber r= DAOs.getSubscriberDAO().GetSubscriberById(s.getNumber());
		

		assertTrue(r.getNumberOfBookLoans()==0);
		Media a= DAOs.getMediaDAO().get(b.getMediaNumberId());
		assertTrue(a.getNumberAvailableExemplary()==3);
		t.commit();
		HibernateUtil.closeSession();
		
		
	}
	
	
	
	@Test
	public void testReservation() throws BadParametersException, MediaExistsException,
		  SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException{
				Session session = HibernateUtil.openSession();
				Media v;
				Subscriber w,x,y,z;
				Transaction t = session.beginTransaction();	
				lib.addMedia(b);
				lib.addSubscriber(s);
				lib.addSubscriber(s1);
				lib.addSubscriber(s2);
				lib.addSubscriber(s3);
				t.commit();
				HibernateUtil.closeSession();
				
				session = HibernateUtil.openSession();
				t = session.beginTransaction();	
				 v= DAOs.getMediaDAO().get(b.getMediaNumberId());
				 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
				 x= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s1.getLastName());
				 y= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s2.getLastName());
				 z= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s3.getLastName());
				assertTrue(v.getNumberAvailableExemplary()==3);
				assertTrue(v.getReservations().size()==0);
				assertTrue(w.getReservations().size()==0);
				assertTrue(x.getReservations().size()==0);
				assertTrue(y.getReservations().size()==0);
				assertTrue(z.getReservations().size()==0);
				lib.lend(s1, b);
				lib.lend(s2, b);
				lib.lend(s3, b);
				t.commit();
				HibernateUtil.closeSession();
				//
				session = HibernateUtil.openSession();
				t = session.beginTransaction();	
				 v= DAOs.getMediaDAO().get(b.getMediaNumberId());
				assertTrue(v.getNumberAvailableExemplary()==0);
				assertTrue(v.getReservations().size()==0);
				lib.reserve(s, b);
				t.commit();
				HibernateUtil.closeSession();
				
			
			session = HibernateUtil.openSession();
			t = session.beginTransaction();	
			v= DAOs.getMediaDAO().get(b.getMediaNumberId());
			w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
			assertTrue(v.getNumberAvailableExemplary()==0);
			assertTrue(v.getReservations().size()==1);
			assertTrue(w.getReservations().size()==1);
			t.commit();
			HibernateUtil.closeSession();
		
	}
	
	@Test(expected = ChildLoanException.class)
	public void testReservation2() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException{
		s= new Subscriber("Iullia","Petrova", new GregorianCalendar(2007, Calendar.JULY, 10), new GregorianCalendar(2012, Calendar.MAY, 02),null); 
		Media v;
		Subscriber w,x,y,z;
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addMedia(d);
		lib.addSubscriber(s);
		lib.addSubscriber(s1);

		t.commit();
		HibernateUtil.closeSession();
		
	     session = HibernateUtil.openSession();
		 t = session.beginTransaction();	
		lib.reserve(s, b);
		lib.lend(s1, d);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		
		 t = session.beginTransaction();	
		 v= DAOs.getMediaDAO().get(d.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(w.getReservations().size()==1);
		assertTrue(v.getNumberAvailableExemplary()==2);
		lib.reserve(s, d);
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test
	public void testCancelReservation() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException {
		// TODO Auto-generated method stub
		Media v;
		Subscriber w,x,y,z;
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		 t = session.beginTransaction();	
		lib.reserve(s, b);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		 t = session.beginTransaction();	
		 v= DAOs.getMediaDAO().get(b.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(w.getReservations().size()==1);
		assertTrue(v.getReservations().size()==1);
		assertTrue(v.getNumberAvailableExemplary()==2);
		lib.cancelReservation(w, v);	
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		 t = session.beginTransaction();	
		 v= DAOs.getMediaDAO().get(b.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		 assertTrue(w.getReservations().size()==0);
		assertTrue(v.getReservations().size()==0);
		assertTrue(v.getNumberAvailableExemplary()==3);
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test
	public void testExtendDateReturn1() throws BadParametersException, MediaExistsException,
			  SubscriberExistsException, LentMediaException, ChildLoanException, ExtensionBackDateException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		Media u= DAOs.getMediaDAO().get(b.getMediaNumberId());
		Subscriber v= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(v.getCurrentloans().size()==0);
		assertTrue(u.getNumberAvailableExemplary()==3);
		t = session.beginTransaction();	
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
		//
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		Subscriber p= DAOs.getSubscriberDAO().GetSubscriberById(s.getNumber());
		assertTrue(p.getNumberOfBookLoans()==1);
		Scanner sc = new Scanner(System.in);
		System.out.println("Veuillez saisir un l'id de l'exemplaire:");
		String str = sc.nextLine();
		lib.extendDateReturnMedia(str);
		t.commit();
		HibernateUtil.closeSession();
		}
	
	@Test(expected = ExtensionBackDateException.class)
	public void testExtendDateReturn2() throws BadParametersException, MediaExistsException,
			  SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException, ExtensionBackDateException{
		Media u;
		Subscriber w,x,y,z;
		
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b2);
		lib.addSubscriber(s);
		lib.addSubscriber(s1);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
	    u= DAOs.getMediaDAO().get(b2.getMediaNumberId());
		w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		assertTrue(w.getCurrentloans().size()==0);
		assertTrue(u.getNumberAvailableExemplary()==1);
		t = session.beginTransaction();	
		lib.lend(s, b2);
		lib.reserve(s1, b2);
		t.commit();
		HibernateUtil.closeSession();
		//
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		assertTrue(b2.getLentExemplariesNumber()==1);
		assertTrue(b2.getReservations().size()==1);
		assertTrue(b2.getNumberAvailableExemplary()==0);
		assertTrue(s.getCurrentloans().size()==1);
		Scanner sc = new Scanner(System.in);
		System.out.println("Veuillez saisir un l'id de l'exemplaire:");
		String str = sc.nextLine();
		lib.extendDateReturnMedia(str);
		t.commit();
		HibernateUtil.closeSession();
		}
	
	@Test
	public void testPrintCurrentLoans() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addMedia(d);
		lib.addSubscriber(s);
		lib.addSubscriber(s1);
		lib.addSubscriber(s2);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.lend(s, d);
		lib.lend(s1, b);
		lib.lend(s2, d);
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.printCurrentLoans();
		t.commit();
		HibernateUtil.closeSession();
		
		

	}
	
	@Test
	public void testPrintCurrentReservations() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addMedia(d);
		lib.addSubscriber(s);
		lib.addSubscriber(s1);
		lib.addSubscriber(s2);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.reserve(s, d);
		lib.reserve(s1, b);
		lib.reserve(s2, d);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.printCurrentReservations();
		t.commit();
		HibernateUtil.closeSession();
	
	}
	
	@Test
	public void testSearchMediaByName() throws BadParametersException, MediaExistsException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addMedia(d);
		lib.addMedia(b1);
		lib.addMedia(b2);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.searchMediaByName("le Mystère du code");
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test(expected = MediaExistsException.class)
	public void testSearchMediaById() throws BadParametersException, MediaExistsException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addMedia(d);
		lib.addMedia(b1);
		lib.addMedia(b2);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.searchMediaById("20127448");
		lib.searchMediaById("0123456");
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test
	public void testSearchSubscriberByName() throws BadParametersException, SubscriberExistsException, MediaExistsException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		s.getEntitles().add(e);
		lib.addSubscriber(s);
		lib.addSubscriber(s3);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.searchSubscriberByName("ADOPO");
		t.commit();
		HibernateUtil.closeSession();
	}
	
	@Test(expected = SubscriberExistsException.class)
	public void testSearchSubscriberById() throws BadParametersException, MediaExistsException, SubscriberExistsException{
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addSubscriber(s);
		lib.addSubscriber(s2);
		lib.addSubscriber(s3);
		t.commit();
		HibernateUtil.closeSession();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.searchSubscriberById(2);
		lib.searchSubscriberById(7);
		t.commit();
		HibernateUtil.closeSession();
		
	}
	
	@Test
	public void loanBackReserve() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException, ExtensionBackDateException{
		Media u,x;
		Subscriber w;
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		System.out.println("Ajout médias et d'abonnés") ;
		lib.addMedia(b);
		s.getEntitles().add(e);
		lib.addMedia(d);
		lib.addSubscriber(s);
		System.out.println(b.toString());
		System.out.println(d.toString());
		System.out.println(e.toString());
		System.out.println(s.toString());
		t.commit();
		HibernateUtil.closeSession();
		
		System.out.println("Regarder PGADMIN") ;
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		u= DAOs.getMediaDAO().get(b.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		 
		System.out.println("emprunt d'un média ") ;
		//emprunt
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
		
		System.out.println("Regarder PGADMIN") ;
		str = sc.nextLine();
		
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		System.out.println("réservation d'un média") ;
		 x= DAOs.getMediaDAO().get(d.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		 //reservation
		lib.reserve(w, x);
		t.commit();
		HibernateUtil.closeSession();
		session= HibernateUtil.openSession();
		
		System.out.println("Regarder PGADMIN") ;
		str = sc.nextLine();
		
		t=session.beginTransaction();
		System.out.println("Affichage des emprunts et reservations") ;
		 u= DAOs.getMediaDAO().get(b.getMediaNumberId());
		 x= DAOs.getMediaDAO().get(d.getMediaNumberId());
		 w= DAOs.getSubscriberDAO().GetSingleSubscriberByLastName(s.getLastName());
		//affiche emprunts en cours dans la bibliothèque
		lib.printCurrentLoans();
		//affiche reservations en cours dans la bibliothèque
		lib.printCurrentReservations();
		System.out.println("Etendre la date de retour et annuler réservation\n") ;
		System.out.println("entrer l'exemplaire à prolonger la date de retour") ;
		str = sc.nextLine();
		//prolonger la date de retour
		lib.extendDateReturnMedia(str);
		//annuler la réservation
		lib.cancelReservation(w, x);
		t.commit();
		HibernateUtil.closeSession();
		
		System.out.println("Regarder PGADMIN") ;
		str = sc.nextLine();
		
		session= HibernateUtil.openSession();
		t=session.beginTransaction();
		System.out.println("Retour d'un exemplaire") ;
		lib.printCurrentLoans();
		lib.printCurrentReservations();
		//retour de l'exemplaire emprunté
		System.out.println("Entrer l'exemplaire à retourner") ;
		str = sc.nextLine();
		lib.returnMedia(str);
		t.commit();
		HibernateUtil.closeSession();
		
		System.out.println("Regarder PGADMIN") ;
		str = sc.nextLine();
		
		session= HibernateUtil.openSession();
		t=session.beginTransaction();
		lib.printCurrentLoans();
		lib.printCurrentReservations();
		//un enfant qui veut emprunter un DVD
		System.out.println("Un enfant qui veut emprunter un DVD") ;
		lib.lend(e, d);
		t.commit();
		HibernateUtil.closeSession();
	}
	
	@Test
	public void emailReservation() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException, ReservationMediaException{
		b = new mediasManagement.Book("le Mystère du code", authors, categories, new GregorianCalendar(),"2012744",fabric);

		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		lib.addSubscriber(s1);
		t.commit();
		HibernateUtil.closeSession();
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.lend(s1, b);
		lib.reserve(s, b);
		t.commit();
		HibernateUtil.closeSession();
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		assertTrue(b.getNumberAvailableExemplary()==0);
		assertTrue(b.getReservations().size()==1);
		Scanner sc = new Scanner(System.in);
		System.out.println("Veuillez saisir un l'id de l'exemplaire:");
		String str = sc.nextLine();
		lib.returnMedia(str);

		t.commit();
		HibernateUtil.closeSession();
	}
	
	@Test
	public void emailLoan() throws BadParametersException, MediaExistsException, SubscriberExistsException, LentMediaException, ChildLoanException{
		
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();	
		lib.addMedia(b);
		lib.addSubscriber(s);
		t.commit();
		HibernateUtil.closeSession();
		session = HibernateUtil.openSession();
		t = session.beginTransaction();	
		lib.lend(s, b);
		t.commit();
		HibernateUtil.closeSession();
		new LoanReminder();
	}
}
	
